import formModal from "../components/formModal"
import sureModal from "../components/sureModal"
import litterMo from "../components/showModal"
import tipModal from "../components/tipModal/"
import loadingModal from "../components/spinLoading"
import ruleModal from "../components/ruleModal"
import alertModel from "../components/alertModal/"
import tableModal from "../components/tableModal/"
const { env, CurrVersionDev, CurrVersionProc } = require("../../package.json")
const utils = {
    CurrVersion: env == "dev" ? CurrVersionDev : CurrVersionProc,
    dateFormat(_time, format = "yyyy-MM-dd HH:mm:ss") {
        // console.log("_time", _time)
        if (_time == undefined || _time == 0) _time = this.getServerSysTime();
        return new Date(_time * 1000).Format(format);
    },
    sysTimeDiff: 0,
    getServerSysTime() {
        return this.sysTimeDiff + Date.parse(new Date()) / 1000;
    },
    checkDateTime(_time) {
        var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
        var r = _time.match(reg);
        if (r == null) return 0;
        var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
        if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6] && d.getSeconds() == r[7]) {
            return this.sysTimeDiff + Date.parse(new Date(_time)) / 1000;
        } else {
            return 0;
        }
    },
    async showFormPage(options, info, format, ruleValidate, callBack, onSelectCallBack) {
        /**options
         * title :标题
         * width :宽度
         * closeable :是否显示关闭按钮
         * maskCloseable :点击遮罩是否关闭
         * okText: 确定按钮文字
         * cancenlText :取消按钮文字,
         * 
         * callBack:  确定回调参数
         * onSelectCallBack: 监听某个选项的回调
         *  info: { test: 123 },
            format: {
                test: {
                    labelName: "测试",
                    labelType: "text"
                }
            }
            ruleValidate: {
                test: [
                    { required: true,message: '测试帐号不能为空', trigger: 'blur' }
                ],
            },
         * **/
        if (typeof options != "object") return;
        this.showLoading();
        await this.sleepMs(100);
        options.info = info;
        options.format = format;
        options.rule = ruleValidate;
        options.okFunc = function(data) {
            if (callBack != undefined) callBack(data);
        }
        options.onChange = function(labelChange, value) {
            if (onSelectCallBack != undefined) onSelectCallBack(labelChange, value)
        }
        let uid = await formModal.showFormModal(options);
        return uid;
    },
    hideFormPage(_index) {
        formModal.closeFormModal(_index);
    },
    async showTablePage(options,info) {
        if (typeof options != "object") return;
        this.showLoading();
        await this.sleepMs(100);
        options.info = info;
        options.onChange = function(page,pageSize) {
            if (onSelectCallBack != undefined) onSelectCallBack(page,pageSize)
        }
        let uid = await tableModal.showTableModal(options);
        return uid;
    },
    hideTablePage(_index) {
        tableModal.closeTableModal(_index);
    },
    reloadFormPage(_index) {
        formModal.reloadFormModal(_index);
    },
    showSureModal(options, callBack = undefined) {
        if (typeof options != "object") {
            return;
        }
        options.callBack = function(data) {
            if (callBack != undefined) {
                callBack(data)
            }
        }
        sureModal.showModal(options);
    },
    shadowModel(options, callBack = undefined) {
        if (typeof options != "object") {
            return;
        }
        options.callBack = function(data) {
            if (callBack != undefined) {
                callBack(data)
            }
        }
        litterMo.showModal(options);
    },    
    async asyncSureModal(options) {
        return new Promise((resolve, reject) => {
            this.showSureModal(options, function(res) {
                resolve(res)
            })
            this.shadowModel(options, function(res) {
                resolve(res)
            })            
        });
    },
    showTip(tips = "loading") {
        tipModal.open(tips);
    },
    showAlert(options) {
        alertModel.open(options);
    },
    showLoading(tips = "正在加载中...") {
        loadingModal.show(tips);
    },
    hideLoading() {
        loadingModal.close();
    },
    cloneObj(obj) {
        return JSON.parse(JSON.stringify(obj));
    },
    arraysEqual(a, b, needSort = false) {
        if (a === b) return true;
        if (a == null || b == null) return false;
        if (a.length != b.length) return false;

        // If you don't care about the order of the elements inside
        // the array, you should sort both arrays here.
        if (needSort) {
            a.sort();
            b.sort();
        }
        for (var i = 0; i < a.length; ++i) {
            if (a[i] !== b[i]) return false;
        }
        return true;
    },
    async sleepMs(ms) {
        return new Promise((resolve, reject) => {
            setTimeout(resolve, ms);
        })
    },
    toDecimal2(number) {
        let n = Math.floor(number * 100)
        let s = n / 100;
        return s;
    },
    toRoundDecimal2(number) {
        let n = Math.round(number * 100)
        let s = n / 100;
        return s;
    },
    showRuleModal(options) {
        if (typeof options != "object") {
            return;
        }
        console.log("options", options)
        let _uid = ruleModal.showModal(options)
        return _uid;
    },
    closeRuleModal(_index) {
        ruleModal.closeModal(_index)
    }
}
export default utils;